package org.vacoor.ticket.handler.impl;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.vacoor.ticket.Constant;
import org.vacoor.ticket.bean.Station;
import org.vacoor.ticket.bean.param.impl.OrderParam;
import org.vacoor.ticket.exception.UnknowException;
import org.vacoor.ticket.handler.ResultHandler;
import org.vacoor.ticket.util.DateUtils;
/**
 * 预订结果处理
 * @author vacoor
 *
 */
public class BookingResultHandler implements ResultHandler<OrderParam> {
	//定义几个结果标识
	public static final int OK_SUCCESS = 0;
	public static final int ERR_UNKNOW = -1;
	public static final int ERR_HAVE_NO_FINISH = 1000;
	public static final int ERR_ILLEGAL_REQ = 2000;
	public static final int ERR_MAINTENANCE = 3000;

	@Override
	public OrderParam handler(byte[] content) {
		OrderParam bInfo = new OrderParam();	
		bInfo.setStateCode(ERR_UNKNOW);
		bInfo.setMsg("未知错误");
		String result;
		try {
			result = new String(content,Constant.ENCODING);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			result = new String(content);
		}
		try{
			if(result.contains("提交订单")) {
				bInfo.setToken($("org.apache.struts.taglib.html.TOKEN",result));
				bInfo.setRemainTicketsDetail($("leftTicketStr",result));
//				System.out.println("leftTicketStr:" + $("leftTicketStr",result));
				bInfo.setTrainDate(DateUtils.parseDate($("orderRequest.train_date",result),new String[]{"yyyy-MM-dd"}));
				bInfo.setTrainId($("orderRequest.train_no",result));
				bInfo.setTrainNo($("orderRequest.station_train_code",result));
				bInfo.setFromStation(Station.getInstance($("orderRequest.from_station_name",result)));
				bInfo.setArriveStation(Station.getInstance($("orderRequest.to_station_name",result)));
				bInfo.setSeatTypeCode($("orderRequest.seat_type_code",result));
				bInfo.setTicketTypeOrderNum($("orderRequest.ticket_type_order_num",result));
				bInfo.setBedLevelOrderNum($("orderRequest.bed_level_order_num",result));
				bInfo.setStartTime($("orderRequest.start_time",result));
				bInfo.setEndTime($("orderRequest.end_time",result));
				bInfo.setCancelFlag($("orderRequest.cancel_flag",result));
				bInfo.setIdMode($("orderRequest.id_mode",result));
				bInfo.setReserveFlag($("orderRequest.reserve_flag",result));
				bInfo.setStateCode(OK_SUCCESS);
				bInfo.setMsg("预订成功");
			}else if (result.contains("系统例行维护时间")) {
				bInfo.setStateCode(ERR_MAINTENANCE);
				bInfo.setMsg("系统例行维护时间");
			}else if(result.contains("非法的订票请求")){
				bInfo.setStateCode(ERR_ILLEGAL_REQ);
				bInfo.setMsg("非法的订票请求");
			}else if(result.contains("未完成订单")){
				bInfo.setStateCode(ERR_HAVE_NO_FINISH);
				bInfo.setMsg("目前您还有未处理的订单,无法继续购票,请您到[未完成订单]进行处理!");
			}else {
				System.out.println("未知错误 at OrderResultHandler:");
				System.out.println(result);
			}
		}catch (Exception e) {
			//e.printStackTrace();
			throw new UnknowException(e.getMessage());
		}
		return bInfo;
	}
	
	//通过表单域的名称获取值
	public static String $(String name,String html) {
		return getInputValueByName(name,html);
	}
	
	public static String getInputValueByName(String name,String html) {
		Pattern pattern = Pattern.compile("<input[^>]*name\\s*=\\s*[\"']*" + name + "[^>]*+>");
		Matcher matcher = pattern.matcher(html);
		if(matcher.find()) {
			String input = matcher.group();
			Pattern valuePattern = Pattern.compile("value\\s*=\\s*\"[^\"]+\"");
			Matcher valueMatcher = valuePattern.matcher(input);
			if(valueMatcher.find()) {
				return valueMatcher.group().replaceAll("value\\s*=\\s*", "").replaceAll("\"", "");
			}
		}
		return "";
	}
}
